import $XEUtils from 'xe-utils'
import $Merge from 'webpack-merge'

const VUE_APP_API_URL = process.env.VUE_APP_API_URL === '-1' ? locat.protocol + '//' + locat.hostname : process.env.VUE_APP_API_URL;
const VUE_APP_API_PORT = process.env.VUE_APP_API_PORT === '-1' ? ':' + locat.port + '/' : process.env.VUE_APP_API_PORT;
const URL_HOST = VUE_APP_API_URL + VUE_APP_API_PORT + process.env.VUE_APP_API_URI;

const VUE_APP_IMG_URL = process.env.VUE_APP_IMG_URL === '-1' ? locat.protocol + '//' + locat.hostname : process.env.VUE_APP_IMG_URL;
const VUE_APP_IMG_PORT = process.env.VUE_APP_IMG_PORT === '-1' ? ':' + locat.port + '/' : process.env.VUE_APP_IMG_PORT;
const IMG_HOST = VUE_APP_IMG_URL + VUE_APP_IMG_PORT + process.env.VUE_APP_IMG_URI;

/**
 * {全局参数，方法}
 *
 * @author: 宋康
 * @date: 2023/1/30
 */
const global = {
  data() {
    return {
      debounceTimer: null,
      debounceTimerObject: {},
      apiUrl: URL_HOST,
      imgUrl: IMG_HOST
    }
  },
  methods: {
    /**
     * {页面防抖}
     *
     * @author: 宋康
     * @date: 2023/2/2
     */
    _debounce(func, delay) {
      if (this.debounceTimer) {
        clearTimeout(this.debounceTimer)
        this.debounceTimer = null;
      }
      this.debounceTimer = setTimeout(() => {
        typeof func === "function" ? func() : ""
      }, delay)
    },
    _debounceT(id, func, delay) {
      if (id) {
        let key = $XEUtils.has(this.debounceTimerObject, id, -1);
        if (-1 !== key) {
          clearTimeout(key)
        }
        const setTimeoutId = setTimeout(() => {
          typeof func === "function" ? func() : ""
        }, delay);
        $XEUtils.set(this.debounceTimerObject, id, setTimeoutId);
      } else {
        throw new Error('定时器ID');
      }
    },
    /**
     * {返回}
     *
     * @author: 宋康
     * @date: 2023/2/2
     */
    _goBack(n) {
      if (!n) {
        n = 0;
      }
      this.$router.go($XEUtils.isInteger(n) ? n : 0);
    },
    /**
     * {替换页面参数Replace}
     *
     * @author: 宋康
     * @date: 2023/2/2
     */
    _mergeUrlReplace($route, query) {
      this.$router.replace({
        query: $Merge($route.query, $XEUtils.isPlainObject(query) ? query : {})
      }).then(r => {
      });
    },
    /**
     * {替换页面参数push}
     *
     * @author: 宋康
     * @date: 2023/2/2
     */
    _mergeUrlPush($route, query) {
      this.$router.push({
        query: $Merge($route.query, $XEUtils.isPlainObject(query) ? query : {})
      }).then(() => {
      });
    },
    /**
     * {跳转登录}
     *
     * @author: 宋康
     * @date: 2023/2/2
     */
    _toLogin() {
      this.$router.push({
        path: '/login',
        query: {
          t: Date.now(),
          furi: encodeURI(this.$route.path)
        }
      })
    },
    /**
     * {路由跳转 path}
     *
     * @author: 宋康
     * @date: 2023/2/7
     */
    _routerPushPath(path, query) {
      this.$router.push({
        path: path,
        query: {
          t: Date.now(),
          m: this.$route.query.m || '0',
          ml: this.$route.query.ml || '0',
          ...query
        }
      })
    },
    /**
     * {路由跳转 name}
     *
     * @author: 宋康
     * @date: 2023/2/7
     */
    _routerPushName(name, query) {
      this.$router.push({
        name: name,
        query: {
          t: Date.now(),
          m: this.$route.query.m || '0',
          ml: this.$route.query.ml || '0',
          ...query
        }
      })
    },
    /**
     * {成功消息}
     * @param msg 消息
     * @param type 通知方式 1右上，2右下，3左下 4左上 默认 消息提示
     * @author: 宋康
     * @date: 2023/2/15
     */
    _success(msg, type) {
      const message = msg || '操作成功';
      if (type) {
        notify(this, message, '1', type);
      } else {
        this.$message({
          showClose: true,
          message: message,
          type: 'success'
        });
      }
    },
    /**
     * {失败消息}
     * @param msg 消息
     * @param type 通知方式 1右上，2右下，3左下 4左上 默认 消息提示
     * @date: 2023/2/15
     */
    _error(msg, type) {
      const message = msg || '操作失败';
      if (type) {
        notify(this, message, '2', type);
      } else {
        this.$message({
          showClose: true,
          message: message,
          type: 'error'
        });
      }
    },
    /**
     * {警告消息}
     * @param msg 消息
     * @param type 通知方式 1右上，2右下，3左下 4左上 默认 消息提示
     * @author: 宋康
     * @date: 2023/2/15
     */
    _warning(msg, type) {
      const message = msg || '操作警告';
      if (type) {
        notify(this, message, '3', type);
      } else {
        this.$message({
          showClose: true,
          message: message,
          type: 'warning'
        });
      }
    }
  }
}

/**
 * {通知框}
 *
 * @author: 宋康
 * @date: 2023/2/15
 */
function notify(that, msg, type, orientation) {
  // 弹出倾向
  let title = '';
  let a = '';
  switch (type) {
    case '1':
      a = 'success';
      title = '成功';
      break;
    case '2':
      a = 'error'
      title = '错误';
      break;
    case '3':
      a = 'warning'
      title = '警告';
      break;
    default:
      a = 'info'
      title = '通知';
      break;
  }
  // 弹出方向
  let d = ''
  switch (orientation) {
    case '2':
      d = 'bottom-right';
      break;
    case '3':
      d = 'bottom-left';
      break;
    case '4':
      d = 'top-left';
      break;
    default:
      d = 'top-right';
      break;
  }

  that.$notify({
    title: title,
    message: msg,
    type: a,
    position: d
  });
}

export default global
